﻿using System.Collections.Generic;
using System.Linq;
using DomainModel;
using Repository.Abstract;

namespace Repository.NHibernate
{
    public class UserRepository : Repository<User>, IUserRepository
    {
        public User GetByLogin(string login)
        {
            using (Session)
            {
                return Session.QueryOver<User>().Where(u => u.Login == login).SingleOrDefault();
            }
        }

        public User GetByEmail(string email)
        {
            using (Session)
            {
                return Session.QueryOver<User>().Where(u => u.Email== email).SingleOrDefault();
            }
        }

        public IEnumerable<User> GetByRole(Role role)
        {
            using (Session)
            {
                return Session.QueryOver<User>().Where(u => ((List<Role>) u.Roles).Contains(role)).List<User>();
            }
        }
    }
}